package com.art.sunflower.controller;

import com.art.sunflower.exception.*;
import com.art.sunflower.mapper.ClassesMapper;
import com.art.sunflower.pojo.Enroll;
import com.art.sunflower.service.ClassesService;
import com.art.sunflower.util.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping(value = "/classes")
@Api(value = "课程班控制器")
public class ClassesController {
    @Resource
    ClassesService classesService;
    @Resource
    ClassesMapper classesMapper;

    @ResponseBody
    @RequiresRoles(value = {"admin"})
    @PostMapping()
    @ApiOperation("添加一条班级预设")
    public Message insertClasses(@RequestParam("classes_name") String classesName, @RequestParam("course_id") int courseId, @RequestParam("teacher_id") int teacherId, @RequestParam("startweek") String startweek,
                                 @RequestParam("endweek") String endweek, @RequestParam("truefee") int truefee, @RequestParam("maxNum") int maxNum) {
        int count = classesService.insertClasses(classesName, courseId, teacherId, startweek, endweek, truefee, maxNum);
        if (count > 0) {
            return Message.createSuc("添加成功");
        }
        return Message.createErr("添加失败");
    }

    @ResponseBody
    @RequiresRoles(value = {"admin"})
    @PutMapping()
    @ApiOperation("修改一条班级预设信息")
    public Message updateClasses(@RequestParam("id") int id, @RequestParam("classes_name") String classesName, @RequestParam("course_id") int courseId, @RequestParam("teacher_id") int teacherId, @RequestParam("startweek") String startweek,
                                 @RequestParam("endweek") String endweek, @RequestParam("truefee") int truefee, @RequestParam("maxNum") int maxNum) {
        int count = classesService.updateClasses(id, classesName, courseId, teacherId, startweek, endweek, truefee, maxNum);
        if (count > 0) {
            return Message.createSuc("修改成功");
        }
        return Message.createErr("修改失败");
    }

    @ResponseBody
    @RequiresRoles(value = {"admin"})
    @DeleteMapping("/{id}")
    @ApiOperation("删除一条班级预设信息")
    public Message deleteClasses(@PathVariable int id) {
        int count = classesService.deleteClasses(id);
        if (count > 0) {
            return Message.createSuc("删除成功");
        }
        return Message.createErr("删除失败");
    }

    @ResponseBody
    @RequiresRoles(value = {"admin"})
    @GetMapping("/{id}")
    @ApiOperation("通过编号查找预设班级信息")
    public Message getOneClasses(@PathVariable int id) {
        if (classesService.selectOneClasses(id) == null) {
            return Message.createErr("预设班级不存在，查找失败");
        } else {
            Map<String, Object> selectOneClasses = classesService.selectOneClasses(id);
            Map<String, Object> selectOneClasses2 = classesService.selectOneClasses2(id);
            selectOneClasses.put("courseName", selectOneClasses2.get("courseName"));
            return Message.createSuc(selectOneClasses);
        }
    }

    @ResponseBody
    @RequiresRoles(value = {"admin"})
    @GetMapping("list")
    @ApiOperation("分页获取预设的列表")
    public Message listAllClasses(@RequestParam Integer pagenum, @RequestParam Integer pagesize) {
        try {
            return Message.createSuc(classesService.listAllClasses(pagenum, pagesize));
        } catch (BusyException e) {
            e.printStackTrace();
            return Message.createErr("获取失败");
        }
    }

    @ResponseBody
    @RequiresRoles(value = {"admin"})
    @GetMapping("openClassesList")
    @ApiOperation("分页获取开班信息列表")
    public Message listAllOpenClasses(@RequestParam Integer pagenum, @RequestParam Integer pagesize) {
        return Message.createSuc(classesService.listAllOpenClasses(pagenum, pagesize));
    }

    @ResponseBody
    @RequiresRoles(value = {"admin"})
    @GetMapping("openClasses/{id}")
    @ApiOperation("通过id获取开班信息")
    public Message getOneOpenClasses(@PathVariable int id) {
        if (classesService.selectOneOpenClasses(id) == null) {
            return Message.createErr("预设班级不存在，查找失败");
        } else {
            Map<String, Object> selectOneOpenClasses = classesService.selectOneOpenClasses(id);
            Map<String, Object> selectOneOpenClasses2 = classesService.selectOneOpenClasses(id);
            selectOneOpenClasses.put("course_name", selectOneOpenClasses.get("course_name"));
            return Message.createSuc(selectOneOpenClasses);
        }
    }

    @ResponseBody
    @RequiresRoles(value = {"admin"})
    @DeleteMapping("openClasses/{id}")
    @ApiOperation("删除开班课程班级")
    public Message deleteOpenClasses(@PathVariable int id) {

        if (classesService.deleteOpenClasses(id) == 1) {
            return Message.createSuc("删除成功");//删除成功
        }
        return Message.createErr("删除失败");//删除失败
    }

    @ResponseBody
    @RequiresRoles(value = {"admin"})
    @PostMapping("openClasses")
    @ApiOperation("增加开班课程班级")
    public Message addOpenClasses(@RequestParam("classes_name") String classesName, @RequestParam("course_id") int courseId, @RequestParam("teacher_id") int teacherId, @RequestParam("startweek") String startweek,
                                  @RequestParam("endweek") String endweek, @RequestParam("truefee") int truefee, @RequestParam("maxNum") int maxNum) {
        if (classesService.addOpenClasses(classesName, courseId, teacherId, startweek, endweek, truefee, maxNum) == 1) {
            return Message.createSuc("添加成功");//添加成功
        }
        return Message.createErr("添加失败");//添加失败
    }

    @ResponseBody
    @RequiresRoles(value = {"admin"})
    @PutMapping("openClasses")
    @ApiOperation("修改开班课程班级的信息")
    public Message updateOpenClasses(@RequestParam("id") int id, @RequestParam("classes_name") String classesName, @RequestParam("course_id") int courseId, @RequestParam("teacher_id") int teacherId, @RequestParam("startweek") String startweek,
                                     @RequestParam("endweek") String endweek, @RequestParam("truefee") int truefee, @RequestParam("maxNum") int maxNum) {
        if (classesService.updateOpenClasses(id, classesName, courseId, teacherId, startweek, endweek, truefee, maxNum) == 1) {
            return Message.createSuc("修改成功");//修改成功
        }
        return Message.createErr("修改失败");//修改失败
    }

    @ResponseBody
    @RequiresRoles(value = {"admin"})
    @PutMapping("openClassesByCheckStatus")
    @ApiOperation("开班")
    public Message openClasses(@RequestParam("id") int id, @RequestParam("trueNum") int trueNum, @RequestParam("maxNum") int maxNum, @RequestParam("status") boolean status) {
        if (classesService.openClasses(id, trueNum, maxNum, status) == 1) {
            return Message.createSuc("开班成功");//开班成功
        }
        return Message.createErr("开班失败");//开班失败
    }

    @RequiresRoles(value = {"teacher"})
    @GetMapping("classesListByTeacher")
    @ApiOperation("分页获取教学班列表")
    public Message findClassesByTeacher(@RequestParam("teacher_id") int teacherId, @RequestParam Integer pagenum, @RequestParam Integer pagesize) {

        return Message.createSuc(classesService.findClassesByTeacher(teacherId, pagenum, pagesize));
    }

    @PostMapping("/enroll")
    @ApiOperation("报名")
    public Message enroll(@RequestBody Enroll enroll) {

        String msg = "报名成功";
        try {
            classesService.enroll(enroll);
            Message.createSuc(msg);
        } catch (EnableClassesException e) {
            e.printStackTrace();
            msg = "未开班";
        } catch (EnrollMaxException e) {
            e.printStackTrace();
            msg = "招生已满";
        } catch (NullClassesException e) {
            e.printStackTrace();
            msg = "课程不存在";
        } catch (InconformityEnrollException e) {
            e.printStackTrace();
            msg = "不符合要求";
        }
        return Message.createErr(msg);
    }

    @GetMapping("/all/{id}")
    @ApiOperation("通过id获取课程信息")
    public Message getClasses(@PathVariable Integer id) {
        String msg;
        try {
            return Message.createSuc(classesService.getClasses(id));
        } catch (NullClassesException e) {
            e.printStackTrace();
            msg = "课程不存在";
        }
        return Message.createErr(msg);
    }

}
